package com.sec.etech.sys.cal.controller;

import cn.hutool.json.JSONObject;
import com.sec.etech.sys.cal.service.WorkCalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.openbpm.base.api.response.impl.ResultMsg;
import org.openbpm.base.rest.ControllerTools;
import org.openbpm.base.rest.util.RequestUtil;
import org.openbpm.form.util.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
@RequestMapping({"/bpm/workcal"})
@Api(description = "工作日历接口")
public class WorkCalController extends ControllerTools {
    @Resource
    WorkCalService workCalService;

    // 工作日历计算
    @RequestMapping(value = {"getRemainWorkDays"}, method = {RequestMethod.POST})
    @ApiOperation(value = "获取工作日剩余天数", notes = "获取剩余天数，传入开始时间和需要工作天数，返回json包含期望结束时间和剩余工作天数")
//    @ApiImplicitParams({@ApiImplicitParam(dataType = "String", name = "startDay", value = "开始时间"), @ApiImplicitParam(dataType = "int", name = "workDays", value = "工作日天数")})
    public ResultMsg<JSONObject> getRemainWorkDays(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = new JSONObject();
        Date startDay = RequestUtil.getDate(request, "startDay");
        int workDays = RequestUtil.getInt(request, "workDays");
        Date endDay = workCalService.getWorkDayByDay(startDay, workDays);
        int remainDays = workCalService.getWorkDaysByTime4remainDay(new Date(), endDay);
        result.put("endDay", endDay);
        result.put("remainDays", remainDays);
        return new ResultMsg<>(result);
    }
    @RequestMapping(value = {"getWorkDayByDay"}, method = {RequestMethod.GET})
    @ApiOperation(value = "获取n个工作日后的工作日期", notes = "获取n个工作日后的工作日期，返回工作日期")
    @ApiImplicitParams({@ApiImplicitParam(dataType = "String", name = "startDay", value = "开始时间"), @ApiImplicitParam(dataType = "int", name = "days", value = "工作日天数")})
    public ResultMsg<Date> getWorkDayByDay(@RequestParam String startDay, @RequestParam int days) {
        Date day = workCalService.getWorkDayByDay(DateUtils.convertDateString(startDay), days);
        return new ResultMsg<>(day);
    }
    @RequestMapping(value = {"isWorkDay"}, method = {RequestMethod.GET})
    @ApiOperation(value = "判断当前日期是否工作日", notes = "判断当前日期是否工作日，返回布尔值")
    @ApiImplicitParams({@ApiImplicitParam(dataType = "String", name = "today", value = "当前日期")})
    public ResultMsg isWorkDay(@RequestParam String today) {
        boolean flag = workCalService.isWorkDay(DateUtils.convertDateString(today));
        return new ResultMsg<>(flag);
    }
}
